π How to Route Requests to a Specific Server for Testing
π― Goalβ
When testing changes on a specific server behind a Load Balancer (LB), you don't want to modify all servers. Instead, you can:
- Use a custom header (``) to route traffic to a specific server.
- Modify Nginx on the target server to respond differently when this header is present.
- Use AWS ALB Header-Based Routing to forward requests based on custom headers.
- Use AWS ALB Sticky Sessions to persist a connection to a specific server.
- Use Host File Entry to point to a specific server for local testing.
ποΈ Option 1: Modify Nginx on the Target Serverβ
On your specific server, edit the Nginx configuration file:
sudo nano /etc/nginx/sites-available/default
Add this inside the server {}
block:
if ($http_x_debug_server = "true") {
return 200 "β
This is the debug server!";
}
Save and restart Nginx:
sudo systemctl restart nginx
β
Now, when a request contains X-Debug-Server: true
, Nginx will return a special response.
ποΈ Option 2: Use AWS ALB Header-Based Routingβ
If you are using an AWS Application Load Balancer (ALB), you can set up listener rules to route requests based on a custom header.
Steps to Configure in AWS ALB:β
- Go to AWS Console β EC2 β Load Balancers.
- Select your ALB and navigate to the Listeners tab.
- Edit rules for the listener (usually port
80
or443
). - Add a New Rule:
- Click Insert Rule.
- Add a Condition: "Header" β
X-Debug-Server = true
- Add an Action: Forward to a specific target group that contains only the debug server.
- Save the Rule.
β
Now, requests with X-Debug-Server: true
will go to the selected server.
ποΈ Option 3: Use AWS ALB Sticky Sessionsβ
AWS ALB supports sticky sessions to persist requests to the same target server using cookies.
Steps to Enable Sticky Sessions:β
- Go to AWS Console β EC2 β Target Groups.
- Select the target group linked to your ALB.
- Click the Attributes tab.
- Enable Stickiness and choose Duration-Based Stickiness.
- Set the duration (e.g.,
300
seconds for 5 minutes). - Save changes.
β Now, once a user hits a specific server, subsequent requests will be routed to the same server for the defined duration.
ποΈ Option 4: Use Host File Entry (Local Testing)β
If you only need to test locally, you can modify the host file on your system to point a specific domain to a particular server.
Steps to Modify Host File:β
On Windows:β
- Open Notepad as Administrator.
- Open the file:
C:\Windows\System32\drivers\etc\hosts
- Add the following entry:
192.168.1.100 myapp.example.com
- Save the file.
On macOS/Linux:β
- Open Terminal and run:
sudo nano /etc/hosts
- Add the following line:
192.168.1.100 myapp.example.com
- Save the file (
CTRL + X
, thenY
, thenEnter
).
β
Now, when you visit https://myapp.example.com
, your request will go directly to 192.168.1.100
.
π₯οΈ Step 3: Send a Request with the Custom Headerβ
1οΈβ£ Using a Browser Extension πβ
Since browsers donβt allow custom headers by default, use an extension like:
Steps:β
- Install ModHeader.
- Open the extension and add a new header:
- Header Name:
X-Debug-Server
- Header Value:
true
- Header Name:
- Visit your site:
https://myapp.example.com
- π You should see:
β This is the debug server!
2οΈβ£ Using Chrome DevTools π οΈβ
You can also test manually in Google Chrome:
- Press
F12
(orCtrl + Shift + I
) to open Developer Tools. - Go to the Network tab.
- Click on your request (
myapp.example.com
). - Under Headers, manually add:
X-Debug-Server: true
- Reload the page.
3οΈβ£ Using cURL (Command Line) π»β
You can test using cURL
on your terminal:
curl -H "X-Debug-Server: true" https://myapp.example.com
Expected output:
β
This is the debug server!
β What Happens Without the Header?β
- If you donβt send the header, the request will be handled normally by the Load Balancer and distributed across servers.
- If you send the header, Nginx, ALB, or the host file entry will route the request to the debug server.
π Now you can safely test changes on a single server! π
π₯ Need Help?β
If you face any issues, feel free to ask!π€